<template>
  <div class="con">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div><img src="../assets/鲸鱼.png" />电商后台管理系统</div>
        <div>
          <el-button @click="exit">退出</el-button>
        </div>
      </el-header>
      <!-- 主体 -->
      <el-container>
        <!-- 左侧导航栏 -->
        <el-aside :width="flag ? '64px' : '203px'">
          <!-- 展开收起导航栏的图标 -->
          <div class="tog">
            <i
              :class="flag ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
              @click="flag = !flag"
            ></i>
          </div>
          <!-- 导航列表 -->
          <!-- collapse-transition为false，关闭折叠展开动画 -->
          <el-menu
            default-active="1"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            router
            unique-opened
            :collapse="flag"
            :collapse-transition="false"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menusList"
              :key="item.id"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + obj.path"
                v-for="obj in item.children"
                :key="obj.id"
              >
                <i class="el-icon-menu"></i>{{ obj.authName }}
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容 -->
        <el-main>
          <!-- 欢迎页面 -->
          <div v-if="name == 'home'">
            <el-result
              icon="success"
              title="欢迎进入后台管理系统"
              subTitle="请开始你的操作"
            >
              <!-- <template slot="extra">
                <el-button type="primary" size="medium" @click="$router.push('/')">退出</el-button>
              </template> -->
            </el-result>
          </div>
          <!-- 面包屑 -->
          <el-breadcrumb separator-class="el-icon-arrow-right" v-else>
            <el-breadcrumb-item :to="{ path: '/home' }"
              >首页</el-breadcrumb-item
            >
            <el-breadcrumb-item v-for="item in navList" :key="item">{{
              item
            }}</el-breadcrumb-item>
          </el-breadcrumb>

          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  data() {
    return {
      flag: false, //导航栏折叠展开的状态
      menusList: [], //左侧导航菜单列表
      navList: [], //面包屑的数据
      name: "",
    };
  },
  watch: {
    $route: {
      //监听当前页面的地址信息
      deep: true,
      immediate: true,
      handler(val) {
        this.navList = val.meta.title;
        this.name = val.name;
      },
    }
  },
  created() {

    // 左侧菜单列表
    this.$store.dispatch("getMenusList").then((res) => {
      this.menusList = res;
      localStorage.setItem('menu',JSON.stringify(this.menusList))
      let arr = [];
      function fn(data) {
        data.forEach((item) => {
          if (!item.children.length) {
            let path = item.path.slice(0, 1).toUpperCase() + item.path.slice(1);
            arr.push({
              path: '/'+item.path,
              component: () => import(`../views/${path}.vue`),
            });
          } else {
            fn(item.children);
          }
        });
        return arr;
      }
      let arr1= fn(this.menusList);
      arr1.forEach((item) => {
        this.$router.addRoute("home", item);
      });
      this.$router.addRoute('goods',{path:"/goods/add",name:'add',component: () => import(`../views/add.vue`)})
    });
  },
  mounted() {},
  methods: {
    // 退出后台管理
    exit() {
      // 删除本地的token并跳转到登录页面
      localStorage.removeItem("token");
      this.$router.push("/");
    },
  },
};
</script>

<style scoped lang='scss'>
.el-container,
.con {
  height: 100%;
}
.el-header {
  background-color: rgb(16, 148, 230);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 22px;
  img {
    width: 50px;
    vertical-align: middle;
    margin-right: 10px;
    margin-top: -10px;
  }
}
.tog {
  text-align: center;
  font-size: 22px;
  background: darkblue;
  color: white;
}

.el-aside {
  background-color: #545c64;
  color: #333;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  padding: 20px 40px;
}
.el-breadcrumb {
  margin-bottom: 20px;
}
</style>
